package com.huanzhidadi.hystrix.feign.consumer.controller;

import com.huanzhidadi.hystrix.feign.consumer.feign.HystrixProviderService;
import com.huanzhidadi.hystrix.feign.consumer.util.ServiceInstanceCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class HystrixConsumerController {
    @Resource
    private HystrixProviderService hystrixProviderService;

    @Autowired
    private ServiceInstanceCache serviceInstanceCache;

    @GetMapping("/consumer/payment/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        List<ServiceInstance> instances = serviceInstanceCache.getInstances("hystrix-provider");
        String result = hystrixProviderService.paymentInfo_OK(id);
        return result;
    }

    @GetMapping("/consumer/payment/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        String result = hystrixProviderService.paymentInfo_FAIL(id);
        return result;
    }

}
